package com.panshi.hujin2.httpcrawler.util.outlook;


import java.io.UnsupportedEncodingException;
/**
 * URL 编码解码（用于outlook eurlpostdata转码用）
 * @Author: wangxl
 * @Date: 2018/11/21 10:33
 */
public class UrlUtil {

    private final static String ENCODE = "UTF-8";

    public static String getURLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, ENCODE);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * outlook urlpostdata encode特殊处理
     * @param str
     * @return
     */
    public static String getUrlPostDataEncoderString(String str){
        String result = getURLEncoderString(str);
        //urlpostdata在encode时，空格会被转成“+”，此时替换成“%20”
        //在urlpostdata encode时，括号要求不作处理，所以要反向替换
        if( result != null && result.length()>0){
            result = result.replaceAll("\\+","%20");
            result = result.replaceAll("%28","(");
            result = result.replaceAll("%29",")");
        }
        return result;
    }

    public static void main(String[] args) {
        //String str = "%7B%22__type%22%3A%22GetItemJsonRequest%3A%23Exchange%22%2C%22Header%22%3A%7B%22__type%22%3A%22JsonRequestHeaders%3A%23Exchange%22%2C%22RequestServerVersion%22%3A%22V2017_08_18%22%2C%22TimeZoneContext%22%3A%7B%22__type%22%3A%22TimeZoneContext%3A%23Exchange%22%2C%22TimeZoneDefinition%22%3A%7B%22__type%22%3A%22TimeZoneDefinitionType%3A%23Exchange%22%2C%22Id%22%3A%22Greenwich%20Standard%20Time%22%7D%7D%7D%2C%22Body%22%3A%7B%22__type%22%3A%22GetItemRequest%3A%23Exchange%22%2C%22ItemShape%22%3A%7B%22__type%22%3A%22ItemResponseShape%3A%23Exchange%22%2C%22BaseShape%22%3A%22IdOnly%22%2C%22AddBlankTargetToLinks%22%3Atrue%2C%22BlockContentFromUnknownSenders%22%3Afalse%2C%22BlockExternalImagesIfSenderUntrusted%22%3Atrue%2C%22ClientSupportsIrm%22%3Atrue%2C%22CssScopeClassName%22%3A%22rps_f624%22%2C%22FilterHtmlContent%22%3Atrue%2C%22FilterInlineSafetyTips%22%3Atrue%2C%22InlineImageCustomDataTemplate%22%3A%22%7Bid%7D%22%2C%22InlineImageUrlOnLoadTemplate%22%3A%22InlineImageLoader.GetLoader().Load(this)%22%2C%22InlineImageUrlTemplate%22%3A%22data%3Aimage%2Fgif%3Bbase64%2CR0lGODlhAQABAIAAAAAAAP%2F%2F%2FyH5BAEAAAEALAAAAAABAAEAAAIBTAA7%22%2C%22MaximumBodySize%22%3A2097152%2C%22MaximumRecipientsToReturn%22%3A20%2C%22ImageProxyCapability%22%3A%22OwaAndConnectorsProxy%22%2C%22AdditionalProperties%22%3A%5B%7B%22__type%22%3A%22PropertyUri%3A%23Exchange%22%2C%22FieldURI%22%3A%22CanDelete%22%7D%2C%7B%22__type%22%3A%22ExtendedPropertyUri%3A%23Exchange%22%2C%22PropertySetId%22%3A%2200062008-0000-0000-C000-000000000046%22%2C%22PropertyName%22%3A%22ExplicitMessageCard%22%2C%22PropertyType%22%3A%22String%22%7D%5D%7D%2C%22ItemIds%22%3A%5B%7B%22__type%22%3A%22ItemId%3A%23Exchange%22%2C%22Id%22%3A%22AQMkADAwATNiZmYAZC1kNzg3LWIyMTctMDACLTAwCgBGAAADTxRSr5SNvUS%2Fem2r%2BZ7BnQcAa%2Bg0I8gyVUOlRMiu5V988AAAAgEMAAAAa%2Bg0I8gyVUOlRMiu5V988AACLyyvnAAAAA%3D%3D%22%7D%5D%2C%22ShapeName%22%3A%22ItemNormalizedBody%22%7D%7D";

    }

}
